package com.lanxiao.cloud.modules.oauth.application;

import com.lanxiao.cloud.common.core.exception.BusinessException;
import com.lanxiao.cloud.common.core.service.IOperatorService;
import com.lanxiao.cloud.modules.oauth.application.assembler.OauthAssembler;
import com.lanxiao.cloud.modules.oauth.application.dto.RegisterCmd;
import com.lanxiao.cloud.modules.oauth.application.dto.UserAuthPwdCmdUpdate;
import com.lanxiao.cloud.modules.oauth.domain.service.IUserOauthService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：qiaoliang
 * @date ：2021-09-14
 */
@Component
public class OauthCmdManager {

  private final IOperatorService operatorService;
  private final IUserOauthService userOauthService;
  private final OauthAssembler oauthAssembler;

  public OauthCmdManager(
      IOperatorService operatorService,
      IUserOauthService userOauthService,
      OauthAssembler oauthAssembler) {
    this.operatorService = operatorService;
    this.userOauthService = userOauthService;
    this.oauthAssembler = oauthAssembler;
  }

  /**
   *  注册
   *
   * @author ：qiaoliang
   * @param register : 注册信息
   * @date 2021-09-14 15:47
   */
  @GlobalTransactional
  @Transactional(rollbackFor = {BusinessException.class, RuntimeException.class, Exception.class})
  public void register(RegisterCmd register) {
    userOauthService.saveUserAuth(oauthAssembler.toUserAuth(register), register.getName(), null);
  }

  /**
   *  修改当前用户密码
   *
   * @author ：qiaoliang
   * @param userAuthPwdCmdUpdate : 密码信息
   * @date 2021-10-26 10:23
   */
  public void updatePwd(UserAuthPwdCmdUpdate userAuthPwdCmdUpdate) {
    userOauthService.updatePwd(operatorService.operator(), userAuthPwdCmdUpdate.getNewPwd(),
        userAuthPwdCmdUpdate.getOldPwd());
  }

}
